package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.config.WebConfig;
import com.antdesignbackend.backend.entity.Artist;
import com.antdesignbackend.backend.entity.Rotation;
import com.antdesignbackend.backend.entity.entityVo.ArtistVo;
import com.antdesignbackend.backend.entity.entityVo.ClassifyVo;
import com.antdesignbackend.backend.service.ArtistService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 小程序茶艺师
 */
@Controller
@RequestMapping("/AppArtist")
public class ArtistController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    private Integer menuId;
    private Integer userId;
    private Integer userType;
    private String roleID;

    @Autowired
    ArtistService artistService;

    /**
     *茶艺师管理
     */
    @GetMapping("/index")
    @AuthToken
    public String index() {
        return "artist/index";
    }

    /***
     * 前端分页查询
     * @throws SecurityException
     * @throws IllegalArgumentException
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@RequestBody ArtistVo data, HttpServletRequest request) throws SecurityException, IllegalArgumentException {
        result = new ReturnVO();
        List<ArtistVo> datalist=new ArrayList<>();
        QueryWrapper<ArtistVo> wrapper =new QueryWrapper<ArtistVo>();
        userId=(Integer) request.getSession().getAttribute("UserID");
        roleID=(String) request.getSession().getAttribute("RoleID");
        //正常查询过滤条件
        if(data.getName()!=null && !data.getName().equals("")){
            wrapper.like("name",data.getName());
        }
        if(data.getPhone()!=null && !data.getPhone().equals("")){
            wrapper.eq("phone",data.getPhone());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=artistService.count(wrapper);
        result.setTotal(total);
        //再查询
        if(total>0){
            datalist= artistService.search(wrapper,new Page<>(data.getPageIndex(),data.getPageSize()));
            for (Artist artist:datalist){
                if (artist.getImageUrl() != null && !artist.getImageUrl().equals("")){
                    artist.setImageUrl(WebConfig.host+"images/"+artist.getImageUrl());
                }
            }
        }
        result.setData(datalist);
        return result;
    }
    /**
     * 修改/新增操作的保存
     */
    @PostMapping("/save")
    @ResponseBody
    @AuthToken
    public ReturnVO save(@RequestBody ArtistVo vo) {
        result = new ReturnVO();
        boolean b = false;
        Artist entity = new Artist();
        if(vo.getId()>0) {
            //先获取旧的数据集
            entity= artistService.getById(vo.getId());
        }
        if(vo.getAction().equals("change")) {
            entity.setState(vo.getState());
        }else if(vo.getAction().equals("chan")){
            entity.setIsWork(vo.getIsWork());
        }else if(vo.getAction().equals("certified")){
            entity.setIsCertified(vo.getIsCertified());
        }else {
            //新增，修改
            entity.setName(vo.getName());
            entity.setPhone(vo.getPhone());
            entity.setAmount(vo.getAmount());
            entity.setStar(vo.getStar());
            entity.setSynopsis(vo.getSynopsis());
            entity.setImageUrl(vo.getImageUrl());
            entity.setLng(vo.getLng());
            entity.setLat(vo.getLat());
            entity.setScope(vo.getScope());
        }
        if (vo.getId() == 0) {
            //新增
           /* entity.setCreateTime(new Date());
            entity.setState(1);
            b = artistService.save(entity);*/
        } else{
            //修改
            b = artistService.updateById(entity);
        }
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
        } else {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }

}
